package com.orangelife.mobile.zoning.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.HouseURL;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.zoning.adapter.SelectHousesAdapter;

public class SelectHousesActivity extends OrangeLifeBaseActivity {

	private SelectHousesAdapter<Map<String,Object>> adapter=null;
	private List<Map<String,Object>> listmap;
	private String buildingID=null;
	private ListView lvzoning;
	private LinearLayout ll;
	private TextView title;
	private Dialog dlgLoading;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_zonning);
		Intent intent=getIntent();
		buildingID=intent.getStringExtra("buildingID");
		initView();
		findView();
	}
	
	/**
	 * 设置适配器
	 */
	private void setAdapter() {

		listmap = new ArrayList<Map<String, Object>>();
		adapter = new SelectHousesAdapter<Map<String, Object>>(
				this, listmap);
		
		lvzoning.setAdapter(adapter);
	}
	private void initView() {
		title = (TextView) findViewById(R.id.tv_title);
		lvzoning=(ListView)findViewById(R.id.lv_zoning);
		ll=(LinearLayout)findViewById(R.id.ll_score);

	}

	private void findView() {
		title.setText("选择房号");
		ll.setVisibility(View.INVISIBLE);
		lvzoning.setOnItemClickListener(itemClickLintener);
		setAdapter();
		selectHouses();
	}
	
	private void selectHouses(){
		dlgLoading = DialogHelper.getInstance().createLoadingDialog(SelectHousesActivity.this);
		dlgLoading.show();
		Map<String,Object> map=new HashMap<>();
		map.put("{buildID}", buildingID);
		JSONRequest.getInstance().sendRequest(JSONRequest.GET, HouseURL.APARTMENT, map,handler,1,HttpUtil.ACTION_URI);
	}
	
	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 500:
				ToastHelper.getInstance()._toast(msg.obj.toString());
				DialogHelper.closeDialog(dlgLoading);
				break;
			case 1001:
				ToastHelper.getInstance()._toast(msg.obj.toString());
				isLogin(dlgLoading);
				break;
			case 1:
				dlgLoading.dismiss();
				listmap = JsonReponseHandler.getListFromJson(
						String.valueOf(msg.obj), adapter, dlgLoading);
				if (listmap.size() == 0) {
					ToastHelper
							.getInstance()
							.displayToastWithQuickClose(
									getResources()
											.getString(
													R.string.no_search_building_info));}
				break;

			default:
				break;
			}
		
		}
		
	};
	AdapterView.OnItemClickListener itemClickLintener=new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> aview, View view, int postion,
				long arg3) {
			Intent intent=new Intent();
			intent.putExtra("apartmentName", listmap.get(postion).get("apartmentName").toString());
			intent.putExtra("apartmentID", listmap.get(postion).get("apartmentID").toString());
			setResult(8, intent);
			finish();
		}
	};
}
